/*
 * @Author: 杜家璇
 * @Date: 2023-04-17 21:15:37
 * @LastEditors: 杜家璇
 * @LastEditTime: 2023-07-31 15:49:17
 * @FilePath: \weiNingWeb\src\store\index.js
 * @Description: 
 * 
 * Copyright (c) 2023 by 用户/公司名, All Rights Reserved. 
 */
import Vue from "vue";
import Vuex from "vuex";
import user from "./modules/user"
import _ from "lodash";
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        //这里放全局参数
        user: {},
        token: localStorage.getItem('token') ? localStorage.getItem('token') : '',
        // imgUrl:'http://ht.sxgkj.com/api.php/',
        // 当前商品 下载 打印状态
        // 下载
        isDownload:false,
        // 打印
        isPrinter:false,
        // 上一次的操作
        lastOperation:'',
        // 拍照成功照片
        photograph:'',
        clientId:'',
        // 生成成功的照片
        generatePhoto:'',
        // 任务id
        taskId:'',
        // 订单流水号
        orderNumber:'',
        // 当前订单是否支付完成
        paymentStatus:false,
    },
    mutations: {
        //这里是set方法
        setUser(state, user) {
            var _user = _.merge({}, state.user);
            if (!_.size(_user)) {
                _user = {};
            }
            _.each(user, function(value, key) {
                _user[key] = value;
            })
            localStorage.setItem("user", JSON.stringify(_user));
            state.user = _user;
        },
        setToken(state, token) {
            localStorage.setItem("token", token);
            state.token = token;
        },
        setClientId(state,clientId){
            localStorage.setItem("clientId", clientId);
            state.clientId = clientId;
        },
        // 公共设置数据方法
        setCommonData(state,data){
            if(data.key != 'photograph'){
                localStorage.setItem(data.key, data.value);
            }
            state[data.key] = data.value
        },
        // 设置生成的图片
        setGeneratePhoto(state,data){
            localStorage.setItem('generatePhoto',data);
            state.generatePhoto = data;
        }
    },
    actions: {},
    modules: {
        user
    },
    getters: {
        user(state) {
            if (!_.size(state.user)) {
                var user = {};
                var u = JSON.parse(window.localStorage.getItem("user"));
                if (u) {
                    user = u;
                }
                localStorage.setItem("user", JSON.stringify(user));
                state.user = user;
                return user;
            }
            return state.user
        },
        token(state) {
            return state.token
        }
    },
    plugins: [createPersistedState()]
});